package cn.iocoder.yudao.module.game.controller.admin.gameOverview;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.gameOverview.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameOverview.GameOverviewDO;
import cn.iocoder.yudao.module.game.service.gameOverview.GameOverviewService;

@Tag(name = "管理后台 - 游戏概况")
@RestController
@RequestMapping("/game/overview")
@Validated
public class GameOverviewController {

    @Resource
    private GameOverviewService overviewService;

    @PostMapping("/create")
    @Operation(summary = "创建游戏概况")
    @PreAuthorize("@ss.hasPermission('game:overview:create')")
    public CommonResult<Long> createOverview(@Valid @RequestBody GameOverviewSaveReqVO createReqVO) {
        return success(overviewService.createOverview(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新游戏概况")
    @PreAuthorize("@ss.hasPermission('game:overview:update')")
    public CommonResult<Boolean> updateOverview(@Valid @RequestBody GameOverviewSaveReqVO updateReqVO) {
        overviewService.updateOverview(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除游戏概况")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:overview:delete')")
    public CommonResult<Boolean> deleteOverview(@RequestParam("id") Long id) {
        overviewService.deleteOverview(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得游戏概况")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:overview:query')")
    public CommonResult<GameOverviewRespVO> getOverview(@RequestParam("id") Long id) {
        GameOverviewDO overview = overviewService.getOverview(id);
        return success(BeanUtils.toBean(overview, GameOverviewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得游戏概况分页")
    @PreAuthorize("@ss.hasPermission('game:overview:query')")
    public CommonResult<PageResult<GameOverviewRespVO>> getOverviewPage(@Valid GameOverviewPageReqVO pageReqVO) {
        PageResult<GameOverviewDO> pageResult = overviewService.getOverviewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GameOverviewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出游戏概况 Excel")
    @PreAuthorize("@ss.hasPermission('game:overview:export')")
    @OperateLog(type = EXPORT)
    public void exportOverviewExcel(@Valid GameOverviewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GameOverviewDO> list = overviewService.getOverviewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "游戏概况.xls", "数据", GameOverviewRespVO.class,
                        BeanUtils.toBean(list, GameOverviewRespVO.class));
    }

}